using UnityEngine;  
using System.Collections; 
using System;
using System.IO;
using System.Text;
using UnityEditor;  
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;


namespace AudioStudio
{
    public class AudioUtility
    {


        public static string GetXmlProperty(XElement node, string key)
        {
            var s = node.Descendants(key);
            if(s != null)
            {
                foreach(var i in s)
                {
                    return i.Value;
                }
            }
            return string.Empty;
        }

        public static int GetXmlPropertyAsInteger(XElement node, string key)
        {
            var s = node.Descendants(key);
            if(s != null)
            {
                int outValue = 0;
                foreach(var i in s)
                {
                    int.TryParse(i.Value,out outValue);
                    return outValue;
                }
            }
            return 0;
        }
        
        public static bool IsSoundAnimationEvent(UnityEngine.AnimationEvent animationEvent)
        {
            foreach (var method in typeof(AnimationSound).GetMethods())
            {
                if(method.IsPublic)
                {
                    if (animationEvent.functionName == method.Name)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public static string Combine(string a,string b)
        {
            return Path.Combine(a,b).Replace('\\','/');
        }

        public static byte[] GetByteArrayProperty(SerializedProperty property)
        {
            if (!property.isArray || property.arraySize == 0)
                return null;

            byte[] byteArray = new byte[property.arraySize];

            for(int i = 0; i < byteArray.Length; i++)
            {
                byteArray[i] = (byte)property.GetArrayElementAtIndex(i).intValue;
            }

            return byteArray;
        }

        public static void SetByteArrayProperty(SerializedProperty property, byte[] byteArray)
        {
            if (!property.isArray)
                return;

            SerializedProperty iterator = property.Copy ();

            iterator.arraySize = byteArray.Length;

            while(iterator.name != "data")
            {
                iterator.Next(true);
            }

            for(int i = 0; i < byteArray.Length; i++)
            {
                iterator.intValue = byteArray[i];
                iterator.Next(true); 
            }
        }

        
    }
}